home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
pyxmpp
/
clientstream.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
7KB
|
234 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
__revision__ = '$Id: clientstream.py 652 2006-08-27 19:41:15Z jajcus $'
__docformat__ = 'restructuredtext en'
import logging
from pyxmpp.stream import Stream
from pyxmpp.streambase import BIND_NS
from pyxmpp.streamsasl import SASLNotAvailable, SASLMechanismNotAvailable
from pyxmpp.jid import JID
from pyxmpp.utils import to_utf8
from pyxmpp.exceptions import StreamError, StreamAuthenticationError, FatalStreamError
from pyxmpp.exceptions import ClientStreamError, FatalClientStreamError
class ClientStream(Stream):
def __init__(self, jid, password = None, server = None, port = None, auth_methods = ('sasl:DIGEST-MD5',), tls_settings = None, keepalive = 0, owner = None):
sasl_mechanisms = []
for m in auth_methods:
if not m.startswith('sasl:'):
continue
m = m[5:].upper()
sasl_mechanisms.append(m)
Stream.__init__(self, 'jabber:client', sasl_mechanisms = sasl_mechanisms, tls_settings = tls_settings, keepalive = keepalive, owner = owner)
self.server = server
self.port = port
self.password = password
self.auth_methods = auth_methods
self.my_jid = jid
self.me = None
self._auth_methods_left = None
self._ClientStream__logger = logging.getLogger('pyxmpp.ClientStream')
def _reset(self):
Stream._reset(self)
self._auth_methods_left = []
def connect(self, server = None, port = None):
self.lock.acquire()
try:
self._connect(server, port)
finally:
self.lock.release()
def _connect(self, server = None, port = None):
if not (self.my_jid.node) or not (self.my_jid.resource):
raise ClientStreamError, 'Client JID must have username and resource'
if not server:
server = self.server
if not port:
port = self.port
if server:
self._ClientStream__logger.debug('server: %r', (server,))
service = None
else:
service = 'xmpp-client'
if port is None:
port = 5222
if server is None:
server = self.my_jid.domain
self.me = self.my_jid
Stream._connect(self, server, port, service, self.my_jid.domain)
def accept(self, sock):
Stream.accept(self, sock, self.my_jid)
def _post_connect(self):
if self.initiator:
self._auth_methods_left = list(self.auth_methods)
self._try_auth()
def _try_auth(self):
if self.authenticated:
self._ClientStream__logger.debug('try_auth: already authenticated')
return None
self._ClientStream__logger.debug('trying auth: %r', (self._auth_methods_left,))
if not self._auth_methods_left:
raise StreamAuthenticationError, 'No allowed authentication methods available'
method = self._auth_methods_left[0]
if method.startswith('sasl:'):
if self.version:
self._auth_methods_left.pop(0)
try:
self._sasl_authenticate(self.my_jid.node, None, mechanism = method[5:].upper())
except (SASLMechanismNotAvailable, SASLNotAvailable):
self._ClientStream__logger.debug('Skipping unavailable auth method: %s', (method,))
return self._try_auth()
except:
None<EXCEPTION MATCH>(SASLMechanismNotAvailable, SASLNotAvailable)
None<EXCEPTION MATCH>(SASLMechanismNotAvailable, SASLNotAvailable)
self._auth_methods_left.pop(0)
self._ClientStream__logger.debug('Skipping auth method %s as legacy protocol is in use', (method,))
return self._try_auth()
else:
self._auth_methods_left.pop(0)
self._ClientStream__logger.debug('Skipping unknown auth method: %s', method)
return self._try_auth()
def _get_stream_features(self):
features = Stream._get_stream_features(self)
if self.peer_authenticated:
bind = features.newChild(None, 'bind', None)
ns = bind.newNs(BIND_NS, None)
bind.setNs(ns)
self.set_iq_set_handler('bind', BIND_NS, self.do_bind)
return features
def do_bind(self, stanza):
fr = stanza.get_from()
if fr and fr != self.peer:
r = stanza.make_error_response('forbidden')
self.send(r)
r.free()
return None
resource_n = stanza.xpath_eval('bind:bind/bind:resource', {
'bind': BIND_NS })
if resource_n:
resource = resource_n[0].getContent()
else:
resource = 'auto'
if not resource:
r = stanza.make_error_response('bad-request')
else:
self.unset_iq_set_handler('bind', BIND_NS)
r = stanza.make_result_response()
self.peer.set_resource(resource)
q = r.new_query(BIND_NS, 'bind')
q.newTextChild(None, 'jid', to_utf8(self.peer.as_unicode()))
self.state_change('authorized', self.peer)
r.set_to(None)
self.send(r)
r.free()
def get_password(self, username, realm = None, acceptable_formats = ('plain',)):
_unused = realm
if self.initiator and self.my_jid.node == username and 'plain' in acceptable_formats:
return (self.password, 'plain')
else:
return (None, None)
def get_realms(self):
return [
self.my_jid.domain]
def choose_realm(self, realm_list):
if not realm_list:
return self.my_jid.domain
if self.my_jid.domain in realm_list:
return self.my_jid.domain
return realm_list[0]
def check_authzid(self, authzid, extra_info = None):
if not extra_info:
extra_info = { }
if not authzid:
return 1
if not self.initiator:
jid = JID(authzid)
if not extra_info.has_key('username'):
ret = 0
elif jid.node != extra_info['username']:
ret = 0
elif jid.domain != self.my_jid.domain:
ret = 0
elif not jid.resource:
ret = 0
else:
ret = 1
else:
ret = 0
return ret
def get_serv_type(self):
return 'xmpp'
def get_serv_name(self):
return self.my_jid.domain
def get_serv_host(self):
return self.my_jid.domain
def fix_out_stanza(self, stanza):
if self.initiator:
stanza.set_from(None)
elif not stanza.get_from():
stanza.set_from(self.my_jid)
def fix_in_stanza(self, stanza):
if self.initiator:
Stream.fix_in_stanza(self, stanza)
else:
stanza.set_from(self.peer)